
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for groups_main.
	''' </summary>
	Public Partial Class groups_main
		Inherits BaseWebPage
		Protected GroupsMain As System.Web.UI.WebControls.Label

		Private popularGroups As New ArrayList()
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			'this.pageTemplate.PageTitle = PageTitles.GROUPS_MAIN_PAGE;

			' Featured group
			Dim grp As Group = Groups.GetFeaturedGroup()

			Me.initPageTitles(Constants.GROUPS_MAIN, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			' init tabs
			ucGroupTabs.ActiveTab = 1
			ucGroupTabs.PageName = Constants.GROUPS_MAIN

			If Constants.SHOW_GROUPS <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			If grp <> Nothing Then
				' get group image
				Dim grpPicture As GroupPicture = grp.DefaultPicture
				If grpPicture <> Nothing Then
					hlGroupPicture.ImageUrl = Pictures.GetPictureLargeThumbnailSrcFullURL(grpPicture, "../")
				Else
					hlGroupPicture.ImageUrl = "../images/default_thumbnail.gif"
				End If
				hlGroupPicture.NavigateUrl = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID
				hlGroupPicture.ToolTip = grp.Name
				hlGroupName.Text = grp.Name
				hlGroupName.NavigateUrl = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID

				hlCategory.Text = Constants.CATEGORIES.GetCategoryByID(grp.CatID).Name
				hlCategory.NavigateUrl = "../" + Constants.CATEGORY_GROUPS + "?" + Constants.CATEGORY_PARAM + "=" + grp.CatID

				hlViewGroup.NavigateUrl = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID
				hlJoinGroup.NavigateUrl = "../" + Constants.CONFIRM_JOIN_GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID

				If Not grp.PublicGroup Then
					lblGroupType.Text = Me.GetPageConstText(htPageConstants, "CONST_Private_TEXT")
				End If

				lblCreated.Text = grp.CreationDate.Day + "/" + grp.CreationDate.Month + "/" + grp.CreationDate.Year
				lblLocation.Text = grp.City + ", " + Constants.COUNTRIES.GetCountryByID(grp.CountryID).Name

				If grp.ShortAbout <> Nothing AndAlso grp.ShortAbout.Length > 0 Then
					lblAbout.Text = Server.HtmlEncode(getBeginning(grp.ShortAbout, Constants.MAX_GROUP_ABOUT_LENGTH))
				Else
					lblAbout.Text = "&nbsp;"
				End If

				popularGroups = Groups.LoadPopularGroups()
				BindGrid()
			Else
				hlGroupPicture.Visible = False
				hlGroupName.Visible = False
				hlCategory.Visible = False
				hlViewGroup.Visible = False
				lblSeparator.Visible = False
				hlJoinGroup.Visible = False
				lblGroupType.Visible = False
				lblCreated.Visible = False
				lblLocation.Visible = False
				lblAbout.Visible = False
				lblName.Text = Me.GetPageConstText(htPageConstants, "CONST_NoFeaturedGroup_TEXT")
				lblCategory.Visible = False
				lblType.Visible = False
				lblCreatedDate.Visible = False
				lblLocationName.Visible = False
				lblAboutGroup.Visible = False
				dgPopularGroups.Visible = False
			End If
		End Sub

		Protected Function CreateDataSource() As ICollection
			Dim dt As New DataTable()

			Dim dr As DataRow
			Dim pic As GroupPicture

			dt.Columns.Add(New DataColumn("GroupID", GetType(Integer)))
			dt.Columns.Add(New DataColumn("URL", GetType(String)))
			dt.Columns.Add(New DataColumn("GroupImg", GetType(String)))

			dt.Columns.Add(New DataColumn("GroupURL", GetType(String)))
			dt.Columns.Add(New DataColumn("Name", GetType(String)))
			dt.Columns.Add(New DataColumn("CatURL", GetType(String)))
			dt.Columns.Add(New DataColumn("CatName", GetType(String)))
			dt.Columns.Add(New DataColumn("About", GetType(String)))
			dt.Columns.Add(New DataColumn("JoinURL", GetType(String)))
			dt.Columns.Add(New DataColumn("Members", GetType(Integer)))
			dt.Columns.Add(New DataColumn("Date", GetType(String)))

			' List search results
			Dim curGroupCreationDate As DateTime
			For Each result As Group In popularGroups
				dr = dt.NewRow()
				curGroupCreationDate = result.CreationDate
				dr(0) = result.ID
				dr(1) = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + result.ID
				pic = result.DefaultPicture
				If pic <> Nothing Then
					dr(2) = Pictures.GetPictureSrcFullURL(pic, True, "../")
				Else
					dr(2) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
				End If
				dr(3) = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + result.ID
				dr(4) = result.Name
				dr(5) = "../" + Constants.CATEGORY_GROUPS + "?" + Constants.CATEGORY_PARAM + "=" + result.CatID
				

				dr(6) = Constants.CATEGORIES.GetCategoryByID(result.CatID).Name
				If result.ShortAbout <> Nothing AndAlso result.ShortAbout.Length > 0 Then
					dr(7) = Server.HtmlEncode(getBeginning(result.ShortAbout, Constants.MAX_GROUP_POPULAR_LENGTH))
				Else
					dr(7) = "&nbsp;"
				End If
				dr(8) = "../" + Constants.CONFIRM_JOIN_GROUP + "?" + Constants.GROUP_PARAM + "=" + result.ID
				dr(9) = result.Members.Count
				dr(10) = Constants.ARR_MONTHS(curGroupCreationDate.Month - 1) + " " + curGroupCreationDate.Day + ", " + curGroupCreationDate.Year

				dt.Rows.Add(dr)
			Next
			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindGrid()
			' init datagrid with database data ordered by "orederBy" param
			dgPopularGroups.DataSource = CreateDataSource()
			dgPopularGroups.DataBind()
		End Sub

		Private Sub dgPopularGroups_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
			' Examine if the item created is the header item 
			Dim lit As ListItemType = e.Item.ItemType
			If ListItemType.Header = lit Then
				' Redirect the default header rendering method to our own method 
				e.Item.SetRenderMethodDelegate(New RenderMethod(NewRenderMethod))
			End If
		End Sub

		Private Sub NewRenderMethod(ByVal writer As HtmlTextWriter, ByVal ctl As Control)
			'  We don't need to write the <TR> tag
			'     because it's already written by the writer 
			'     so now we write the Most Popular Groups column 
			writer.Write("<TD class=""data_grid_header_orange"" colspan=""2"" align=""left"" width=""70%"">&nbsp;&nbsp;&nbsp;Most Popular Groups</TD>" & Chr(10) & "")

			dgPopularGroups.HeaderStyle.AddAttributesToRender(writer)

			'***  Render all the cells that was defined
			'     in design time, except the first 2 ones 
			'     because we already rendered them above
			Dim i As Integer = 2
			While i <= ctl.Controls.Count - 1
				ctl.Controls(i).RenderControl(writer)
				System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
			End While

			'  We don't need to write the </TR> close tag
			'  because the writer will do that for us
		End Sub
		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler dgPopularGroups.ItemCreated, AddressOf Me.dgPopularGroups_ItemCreated

		End Sub
#End Region

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






